package com.smartthinking.qualitystores;

import java.util.ArrayList;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class Tab2Fragment extends Fragment {

	String saldo_actual, liquede_con, mv_la, mv, pm24, pm12, linea_articulos, linea_prestamos, ultupdt, almacen, monto, tipo, user, pass, edo, det, 
	todos, todosnew;
	ListView lista1, lista2, lista3;
	public AdaptadorBusqueda0 adaptadorbusqueda0;
	public AdaptadorBusqueda1 adaptadorbusqueda1;
	public AdaptadorBusqueda2 adaptadorbusqueda2;
	StringBuilder builder1 = new StringBuilder();
	StringBuilder builder2 = new StringBuilder();
	StringBuilder builder3 = new StringBuilder();
	ArrayList<String> listultupdt = new ArrayList<String>();
	ArrayList<String> listalmacen = new ArrayList<String>();
	ArrayList<String> listmonto = new ArrayList<String>();

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.tab_fragment2, null);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		initView();
	}

	private void initView() {
		listultupdt.clear();
		listalmacen.clear();
		listmonto.clear();

		SharedPreferences preferencias1 = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
		//		ultupdt= preferencias1.getString("ultupdt","1");
		//		almacen= preferencias1.getString("almacen","1");
		//		monto= preferencias1.getString("monto","1");
		//		tipo= preferencias1.getString("tipo","1");
		todos = preferencias1.getString("Pagos","1");

		SharedPreferences preferencias2 = getActivity().getSharedPreferences("prefs", Context.MODE_PRIVATE);
		todosnew = preferencias2.getString("Pagosnew","1");


		if(preferencias1.getString("Pagos","1").equals(preferencias2.getString("Pagosnew","1"))){

			String temporal[]=todos.split("@");
			for (int i=0;i<temporal.length;i++){
				Log.i("temporal", ""+temporal.length);

				String temporal0[]=temporal[i].trim().split("%");

				ultupdt = temporal0[0].trim();
				Log.i("ultupdt", ""+ultupdt);
				listultupdt.add(ultupdt);
				Log.i("listultupdt",""+listultupdt.size());

				almacen = temporal0[1].trim();
				Log.i("almacen", ""+almacen);
				listalmacen.add(almacen);
				Log.i("listalmacen",""+listalmacen.size());

				monto = temporal0[2].trim();
				Log.i("monto", ""+monto);
				listmonto.add(monto);
				Log.i("listmonto",""+listmonto.size());
				
			}

			lista1 = (ListView)getView().findViewById(R.id.listView1);
			AdaptadorBusqueda0 adapter = new AdaptadorBusqueda0(listultupdt);
			lista1.setAdapter(adapter);
			adapter.notifyDataSetChanged();

			lista2 = (ListView)getView().findViewById(R.id.listView2);
			AdaptadorBusqueda1 adapter1 = new AdaptadorBusqueda1(listalmacen);
			lista2.setAdapter(adapter1);
			adapter1.notifyDataSetChanged();

			lista3 = (ListView)getView().findViewById(R.id.listView3); 
			AdaptadorBusqueda2 adapter2 = new AdaptadorBusqueda2(listmonto);
			lista3.setAdapter(adapter2);
			adapter2.notifyDataSetChanged();
			
		}else{
			
			String temporal[]=todosnew.split("@");
			for (int i=0;i<temporal.length;i++){
				Log.i("temporal", ""+temporal.length);

				String temporal0[]=temporal[i].trim().split("%");

				ultupdt = temporal0[0].trim();
				Log.i("ultupdt", ""+ultupdt);
				listultupdt.add(ultupdt);
				Log.i("listultupdt",""+listultupdt.size());

				almacen = temporal0[1].trim();
				Log.i("almacen", ""+almacen);
				listalmacen.add(almacen);
				Log.i("listalmacen",""+listalmacen.size());

				monto = temporal0[2].trim();
				Log.i("monto", ""+monto);
				listmonto.add(monto);
				Log.i("listmonto",""+listmonto.size());
				
			}
			
			lista1 = (ListView)getView().findViewById(R.id.listView1);
			AdaptadorBusqueda0 adapter = new AdaptadorBusqueda0(listultupdt);
			lista1.setAdapter(adapter);
			adapter.notifyDataSetChanged();

			lista2 = (ListView)getView().findViewById(R.id.listView2);
			AdaptadorBusqueda1 adapter1 = new AdaptadorBusqueda1(listalmacen);
			lista2.setAdapter(adapter1);
			adapter1.notifyDataSetChanged();

			lista3 = (ListView)getView().findViewById(R.id.listView3); 
			AdaptadorBusqueda2 adapter2 = new AdaptadorBusqueda2(listmonto);
			lista3.setAdapter(adapter2);
			adapter2.notifyDataSetChanged();
		}
	}


	public class AdaptadorBusqueda0 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda0(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder{
		public TextView nombre;
	}



	public class AdaptadorBusqueda1 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda1(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder1{
		public TextView nombre;
	}


	public class AdaptadorBusqueda2 extends BaseAdapter {

		private final ArrayList<String> ListaDatos;

		public AdaptadorBusqueda2(ArrayList<String> list) {
			// TODO Auto-generated constructor stub
			this.ListaDatos = list;
		}

		@Override
		public int getCount() {
			return ListaDatos.size();
		}

		@Override
		public Object getItem(int arg0) {
			return ListaDatos.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			View view = arg1;
			Holder holder;
			if(view == null){
				LayoutInflater inflater = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafecha, arg2, false);
				holder = new Holder();
				holder.nombre = (TextView) view.findViewById(R.id.textView1);
				view.setTag(holder);
			}
			else{
				holder = (Holder) view.getTag();

			}
			holder.nombre.setText(ListaDatos.get(arg0));
			return view;
		}
	}

	public static class Holder2{
		public TextView nombre;
	}

	//	private void replaceFragment() {
	//		((BaseContainerFragment)getParentFragment()).replaceFragment(new Tab2AddOnFragment(), true);
	//	}

}
